package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityMjTimeInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoTimeInfo;
import com.ocom.srvpaccess.service.TimeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TimeInfoServiceImpl extends ServiceImpl<IDaoTimeInfo, EntityMjTimeInfo> implements TimeInfoService {


    @Autowired
    private IDaoTimeInfo daoTimeInfo;

    /**
     * 3、从门禁时段表mj_time_info查找记录：
     *
     * @return*/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjTimeInfo> selectGroupNo(Long companyId, Long perGroupNum, Long areaId) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(perGroupNum)){
        query.eq("com_id",companyId);
        }
        if (StringUtils.isNotNull(perGroupNum)){
            query.gt("group_no",perGroupNum);
        }
        query.and(wrapper -> {
            wrapper.and(itemWrapper ->{
                itemWrapper.lt("group_no", 17);
                itemWrapper.eq("area_id", 0);
            });
            wrapper.or(itemWrapper ->{
                itemWrapper.gt("group_no", 16);
                itemWrapper.eq("area_id", areaId);
            });
        });
        query.orderByAsc("group_no").orderByAsc("zone_no").orderByAsc("time_no");
        return daoTimeInfo.selectList(query);
    }

    //从门禁时段表mj_time_info查找满足以下条件的记录，按time_no正向排序
    //com_id= companyID、group_no = role_i、zone_no = zoneNo、area_id = ( role_i < 17 ? 0 : areaNo )
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjTimeInfo> selectTimeInfo(Long companyId, Long groupNo, Long zoneNo, Long areaId,Integer direction) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("group_no",groupNo);
        query.eq("zone_no",zoneNo);
        if (groupNo < 17){
            query.eq("area_id",0);
        }else {
            query.eq("area_id",areaId);
        }
        if (direction==1){
            query.in("direction",0,1);
        }else if (direction==2){
            query.in("direction",0,2);
        }else {
            query.eq("direction",0);
        }
       return daoTimeInfo.selectList(query);
    }

    //c)start_endtime增加：mj_time_info. start_time - mj_time_info. end_time
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjTimeInfo> selectTimeInfo(Long companyId,Long pergroupnum) {
        QueryWrapper<EntityMjTimeInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("group_no",pergroupnum);
        return daoTimeInfo.selectList(query);
    }
}
